home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
INFO
/
PCCDEMO.ZIP
/
COMP1.EXE
/
SBPROG.PRS
< prev
next >
Wrap
Text File
|
1993-12-20
|
8KB
|
210 lines
ÆÄöìâ üïÇÆôäæ ÅæÄåæÇîîêìå ╬╠╬╠╬╠╧╧╬╠╬╠╡
│ Sound cards are becoming
│ increasingly more common in PC's
│ and whilst they make games more
│ enjoyable, and PC music
│ │ │ │ acceptable - it's even more fun
┌─┐┌─┐┌ ┌┌─┐┌─┤ ├─┐│┌─┐┌─┐├ ┌─┐┌─┐│ to use them in programs your own
└─┐│ ││ ││ ││ │ │ ││┌─┤└─┐│ ├─┘│ │ programs. ≤τΣ ∩±εµ±α∞ ≤τα≤
└─┘└─┘└─┘└ └└─┘ └─┘┴└─┘└─┘└─┘└─┘┴ │ σεδδε÷≥ ≥τε÷≥ τε÷ ≤ε ∩δα√
. │ Γ±Σα≤Φ⌡Σ δαß≥ ⌡εΓ σΦδΣ≥, α Γε∞∞εφ
┌─┐┌─┌─┐┌─┐┌─┐┌─┐┌─┬─┐┌─┬─┐┬┌─┐┌─┐ │ σε±∞α≤ σε± ≥≤ε±Φφµ πΦµΦ≤ΦⁿΣπ
│ ││ │ ││ ││ ┌─┤│ │ ││ │ │││ ││ │ │ ≥ε⌠φπ≥.
├─┘┴ └─┘└─┤┴ └─┘└ └ └└ └ └┴└ └└─┤ │
│ │ │ │ This program is writen in
└─┘ └─┘ │ Turbo Pascal, however, once the
│ basics of the program are
│ understood, it can be re-writen
│ in other languages.
ß√ ≥≤Σ⌡Σφ α±φεδπ │ To understand the program, an
│ intermediate level of Turbo
Source Code: SB.PAS │ Pascal experience is required -
although the program is │ The program contains two elements
relatively simple due to its use │ of Turbo Pascal that may be new
of the CT-VOICE driver. This │ to you, pointers and built-in
driver, provided with Sound │ assembly. Following is an
Blaster type cards, provides a │ explanation of these.
simple programming interface to │
the card, bypassing the need to │
control the hardware directly. │
│ ÅÄêìôäæÆ
Using the driver also provides │
greater compatibility. With new │
sound cards only the driver needs │ A pointer is literally a
to be substituted in order for │ variable that points to another
your program to work. │ variable. So instead of storing
│ a number or character string etc,
A file called SB.PAS contains │ it stores the memory location of
the full programs listing, it is │ another variable. Following are
available with this copy of PC │ examples of how to specify a
COMPLETE. │ pointer variable.
│
var
test : pointer;
test1 : ^string;
In the above example, test can point to any variable, while test1 can only
point to a string variable - this is checked by the compiler. The following
program fragment shows an example of their use.
var
a : word;
p1, p2 : ^word;
begin
a := 1;
p1 := @a; {The "@" symbol means "the memory location of"
so, p1 := the memory location of a}
writeln('A = ', a, ' P1^ = ', p1^);
{The "^" symbol means the data stored at the memory
location held by the preceding pointer variable.
So in this case, the data stored at the address
pointed to by p1}
p2 := p1; {Pointers can be assigned in the same way as other
variables. So p2 := p1 means store in p2 the memory
location stored in p1}
writeln('A = ', a, ' P1^ = ', p1^, ' P2^ = ', p2^);
end.
ÅÄêìôäæÆ - âÿìÇîêé òÇæêÇüïäÆ
A dynamic variable is one that can be created, and removed as desired,
allowing the memory to be used for other purposes. In Turbo Pascal they
also have the advantage of being stored on the heap, as against the stack.
The stack is only 64k in size, where as the heap can expand to 640k.
Placing large variables on the heap saves room for small loop control
variables etc on the stack. Following are some examples of their use.
Note - a dynamic variable is always access via a pointer variable.
var
p1, p2 : ^string;
p3 : pointer;
begin
new(p1); {New allocates the memory required}
p1^ := 'Hello'; {Stores 'Hello' in space allocated}
dispose(p1); {Free's up the allocated memory}
getmem(p3, 256); {Allocates 256 bytes, and points p3 to this memory}
p2 := p3; {p2 now points to the same place as p3}
p2^ := 'Hello'; {The allocated memory now contains 'Hello'}
freemem(p3, 256); {De-allocates the 256 bytes}
end.
ôçä üöêïô-êì ÇÆÆäîüïäæ
In Turbo Pascal 6.0 the built-in assembler was added.
This allows assembly language to be included simply in your Turbo Pascal
source code. Here is an example.
begin
writeln('Wow - assembly language');
asm {'asm' specifies the start of any assembly code}
mov ah, 2 {This assembly code moves the cursor to (10,10)}
mov bh, 0
mov dh, 10-1
mov dl, 10-1
int 10h
end; {This 'end' specifies the end of the assembly}
writeln('** Bye **');
end.
If a procedure or function needs only to be assembly only, it can be
declared as an assembly procedure/function, and improves the efficiency
of the code generated. Here is an example.
procedure MoveTo(x, y : byte); assembler; {The 'assembler' declares this
procedure to be an assembly
procedure}
asm {Notice no 'begin' is used}
mov ah, 2
mov bh, 0
mov dh, y {Variables can be included easily}
dec dh
mov dl, x
dec dl
int 10h
end;
In the program "SB.PAS" you will notice that assembly statements are used
extensively, as are pointer and dynamic variables. Hopefully you this will
help you understand their use, and their benefits.
åäìäæÇï ÄÅäæÇôêÄì Äà ÅæÄåæÇî
The CT-VOICE driver is first loaded into memory. It basically provides
a number of "procedures" and "functions" that we require in order to play
VOC files. Once loaded the pointer variable "CTVDriver" points to the start
of the driver in memory. Calling this address will "run" the driver,
however it needs to know what it is we want it to do. Communication to the
driver is via the CPU's registers.
Here is one of the procedures from the program -
> procedure SetBaseAddr(address : word); assembler;
>
> { Sets the base address of the sound card }
>
> asm
> mov bx, 1
> mov ax, address
> call CTVDriver
> end;
First we put 1 into the CPU register "bx". When the driver is called
ie. on the line "call CTVDriver", it looks at the contents of "bx" and
knows that you wish to set the base address of the sound card, and hence
goes to it's "procedure" that does this. The driver then looks at the
"ax" register which contains the address of the sound card. When the
driver needs to return values, the registers are again used.
> function InitSB:boolean; assembler;
>
> { Initialises the sound card }
>
> asm
> mov bx, 3
> call CTVDriver
> cmp ax, 0
> jne @false
> mov ax, word(TRUE)
> jmp @done
> @false:
> mov ax, word(FALSE)
> @done:
> end; { Return is via AX }
In "InitSB" the driver returns the result of initialisation in the "ax"
register. Our Turbo Pascal procedure then returns this result to its caller
again through the "ax" register ñ
Turbo Pascal is a trade mark of Borland International.
Sound Blaster is a trade mark of Creative Labs, Inc.